package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.memberrecharge.MemberRechargeFragment;
import com.zzsy.carosshopprojects.adapter.CardSelectAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.MainCardListBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 选择主卡
 */
public class CardSelectFragment extends BaseFragment {

    public static CardSelectFragment newInstance(String phone, String type) {
        CardSelectFragment cardSelectFragment = new CardSelectFragment();
        Bundle bundle = new Bundle();
        bundle.putString("phone", phone);
        bundle.putString("type", type);
        cardSelectFragment.setArguments(bundle);
        return cardSelectFragment;
    }

    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private CardSelectAdapter cardSelectAdapter;
    private String type;

    @Override
    protected int initLayout() {
        return R.layout.fg_cardselect;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.ChoseMasterCard), getActivity());
        cardSelectAdapter = new CardSelectAdapter(getContext(), null, 0);
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(cardSelectAdapter);
        cardSelectAdapter.setOnItemClickListener((v, position) -> {
            if (!StringUtils.isEmpty(type)) {
                if ("1".equals(type)) {
                    startWithPop(CardReplaceMemberFragment.newInstance(cardSelectAdapter.getListData().get(position)));
                } else if ("2".equals(type)) {
                    String uuid = cardSelectAdapter.getListData().get(position).getUuid();
                    String productId = cardSelectAdapter.getListData().get(position).getProductId();
                    if (!StringUtils.isEmpty(uuid) && !StringUtils.isEmpty(productId)) {
                        startWithPop(FuCardListFragment.newInstance(uuid, productId));
                    } else {
                        ToastUtils.showToast(getContext().getApplicationContext(), "该主卡没有UUID数据！");
                        return;
                    }
                } else if ("3".equals(type)) {
                    MainCardListBean.MyMainCardsBean myMainCardsBean = cardSelectAdapter.getListData().get(position);
                    setReghter(myMainCardsBean);
                }
            }
        });
    }

    @Override
    protected void initData(Context mContext) {
        type = getArguments().getString("type");
    }

    @Override
    protected void loadMethod() {
        getMyMainCardsByPhone();
    }

    private void getMyMainCardsByPhone() {
        RequestUtils.MyMainCardsByPhone(getContext(), DataUtil.getInstance().getCompanyId(), getArguments().getString("phone"), new MyObserver<MainCardListBean>(getContext()) {
            @Override
            public void onSuccess(MainCardListBean result) {
                if (result == null) {
                    ToastUtils.showToast(getContext().getApplicationContext(), "数据返回异常！！");
                    return;
                }
                cardSelectAdapter.setListData(result.getMyMainCards());
                cardSelectAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext().getApplicationContext(), errorMsg);
            }
        });
    }

    /**
     * 跳转充值传递的数据
     */
    private void setReghter(MainCardListBean.MyMainCardsBean myMainCardsBean) {
        startWithPop(MemberRechargeFragment.newInstance("", myMainCardsBean));
    }

}
